<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;
use mdm\admin\components\Configs;
use mdm\admin\components\Helper;
use mdm\admin\models\BizRule;
use mdm\admin\models\searchs\BizRule as BizRuleSearch;
use yii\helpers\ArrayHelper;

class QxRuleController extends \mdm\admin\controllers\RuleController
{

    /**
     * {@inheritdoc}
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
        date_default_timezone_set('PRC');//设置时间时区为中国
        header("Content-type:application/x-www-form-urlencoded text/html; charset=utf-8");//设置格式为utf-8
        header('Access-Control-Allow-Origin:'.\Yii::$app->components['allowOutHttp']);
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:X-Requested-With, origin, content-type, accept');
        header('Access-Control-Allow-Credentials: true');
        return true;
    }

    /**
     * 将数据打包成 layui可以识别的数据格式
     * @param $count
     * @param $all
     * @return array
     */
    public function packData($count,$all){
        $res = [];
        $res['code'] = 0;
        $res['msg'] = "";
        $res['count'] = $count;
        $res['data'] = $all;
        $res['res'] = 1;
        return json_encode((object)$res);
    }

    public function actionList()
    {
        $searchModel = new BizRuleSearch();
        $dataProvider = $searchModel->search(\Yii::$app->request->getQueryParams());
        $all = array_values(ArrayHelper::toArray($dataProvider->allModels,'post'));
        $count = $dataProvider->count;
        //必须要把数组转化为对象，前端固定的格式
        return $this->packData($count,$all);
    }

    /**
     * Creates a new AuthItem model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new BizRule(null);
        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            Helper::invalidate();
            $res['data'] = ArrayHelper::toArray($model->getItem());
            $res['res'] = 1;
        } else {
            $error = array_values($model->getErrors());
            $res['data'] = isset($error[0][0])?$error[0][0]:"";
            $res['res'] = 0;
        }
        return json_encode($res);
    }

    /**
     * Updates an existing AuthItem model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param  string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $res = "";
        if ($model->load(\Yii::$app->getRequest()->post()) && $model->save()) {
            $res['data'] = ArrayHelper::toArray($model->getItem());
            $res['res'] = 1;
        }else{
            $error = array_values($model->getErrors());
            $res['data'] = isset($error[0][0])?$error[0][0]:"";
            $res['res'] = 0;
        }
        return json_encode($res);
    }
    /**
     * Deletes an existing AuthItem model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param  string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        Configs::authManager()->remove($model->item);
        Helper::invalidate();
        $res['data'] = "";
        $res['res'] = 1;
        return json_encode($res);
    }


    /**
     * 前端的 拿到登陆用的token
     * @return boolean
     */
    public function actionFrontCsrfToken()
    {
        $request = \Yii::$app->getRequest();
        if ($request->enableCsrfCookie || $this->enableSession) {
            return $_POST[\Yii::$app->components['request']['csrfParam']] = $request->getCsrfToken(true);
        }
    }


}